package com.lqq.eduservice.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lqq.commonutils.R;
import com.lqq.eduservice.entity.EduTeacher;
import com.lqq.eduservice.entity.vo.TeacherQuery;
import com.lqq.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-26
 */
@RestController
//@CrossOrigin//解决跨域
@RequestMapping("/eduservice/teacher")
@Api(description = "讲师管理")
public class EduTeacherController {
    @Autowired
    private EduTeacherService service;

    //1.查询所有老师
    @ApiOperation("查询所有老师")
    @GetMapping("/findAll")
    public R findAllTeacher() {
        List<EduTeacher> teachers = service.list(null);
        return R.ok().data("items", teachers);
    }

    //2.逻辑删除老师
    @ApiOperation("逻辑删除老师")
    @DeleteMapping("/delete/{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)  @PathVariable("id") String id) {
        boolean flag = service.removeById(id);
        if (flag) {
            return R.ok();
        }
        else {
            return R.error();
        }
    }

    //3.分页查询老师
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable("current") Long current,
                             @PathVariable("limit") Long limit ) {
        Page<EduTeacher> page = new Page<>(current, limit);
        service.page(page, null);
        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return R.ok().data(map);
    }

    //4.按条件分页查询
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") Long current,
                                  @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {

        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        //条件查询，第一个参数为表的字段
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        wrapper.orderByDesc("gmt_create");
        service.page(page, wrapper);

        List<EduTeacher> records = page.getRecords();
        long total = page.getTotal();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return R.ok().data(map);
    }

    //5.添加讲师
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher teacher) {
        boolean save = service.save(teacher);
        if (save) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    //6.根据id查询讲师
    @GetMapping("/getTeacherById/{id}")
    public R getTeacherById(@PathVariable("id") String id) {
        //模拟异常int i = 10/0;
        EduTeacher teacher = service.getById(id);
        return R.ok().data("teacher", teacher);
    }

    //7.修改讲师功能
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody  EduTeacher teacher) {
        boolean flag = service.updateById(teacher);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }

    }

}

